package com.bootdo.clouddo.controller.juejin;

import java.util.List;

import com.bootdo.clouddo.controller.common.CommonController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bootdo.clouddo.domain.juejin.ArticleCollectionDO;
import com.bootdo.clouddo.domain.juejin.CategoryDO;
import com.bootdo.clouddo.domain.juejin.PostDO;
import com.bootdo.clouddo.domain.juejin.UserDO;
import com.bootdo.clouddo.dto.juejin.RequestPostDTO;
import com.bootdo.clouddo.service.juejin.IArticleCollectionService;
import com.bootdo.clouddo.service.juejin.ICategoryService;
import com.bootdo.clouddo.service.juejin.IPostDetailService;
import com.bootdo.clouddo.service.juejin.IPostService;
import com.bootdo.clouddo.service.juejin.IJuejinUserService;
import com.bootdo.clouddo.context.FilterContextHandler;
import com.bootdo.clouddo.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/juejin")
public class PostController extends CommonController {
	
	@Autowired
	private IPostService postService;
	
	@Autowired
	private IPostDetailService detailService;

	@Autowired
	private ICategoryService categoryService;
	
	@Autowired
	private IJuejinUserService userService;
	
	@Autowired
	private IArticleCollectionService collectionService;
	
	@GetMapping("/find")
	public R find(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="12")Integer size, RequestPostDTO params) {
		PageHelper.startPage(page, size);
		return R.data(new PageInfo<>(postService.find(params.getCategory(), params.getWd(), params.getChannel(), page, size)));
	}
	
	@GetMapping("/findByUser/{userId}")
	public R findByUser(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="12")Integer size, @PathVariable String userId) {
		PageHelper.startPage(page, size);
		return R.data(new PageInfo<>(postService.findByUser(userId, page, size)));
	}
	
	@GetMapping("/findTop/{num}")
	public R findTop(@PathVariable Integer num) {
		return R.data(postService.findTop(num));
	}
	
	@GetMapping("/detail/{id}")
	public R detail(@PathVariable String id) {
		PostDO post = postService.get(id);
		if(post == null)
			return R.error("post_not_found");
		post.setDetail(detailService.get(id));
		return R.data(post);
	}
	
	@GetMapping("/categories")
	public R categories() {
		return R.data(categoryService.findAll());
	}
	
	@GetMapping("/user/{id}")
	public R user(@PathVariable String id) {
		UserDO user = userService.get(id);
		if(user == null)
			return R.error("user_not_found");
		List<CategoryDO> categories = postService.findCategoriesByUser(id);
		List<CategoryDO> allCategories = categoryService.findAll();
		if(categories != null && !categories.isEmpty()) {
			categories.stream().forEach(p->{
				for (CategoryDO ca : allCategories) {
					if(StringUtils.equals(ca.getId(), p.getId())) {
						p.setIcon(ca.getIcon());
						break;
					}
				}
			});
		}
		user.setCategories(categories);
		user.setTags(postService.findTagsByUser(id));
		return R.data(user);
	}
	
	@GetMapping("/info/{id}")
	public R info(@PathVariable String id) {
		//查询收藏信息
		ArticleCollectionDO collection = collectionService.findByUidAndAid(getUid(), id);
		return R.ok().put("collection", collection != null ? true : false);
		
	}
	
}
